package com.party.core.service.copartner.impl;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.party.common.paging.Page;
import com.party.core.dao.read.copartner.BrokerageItemReadDao;
import com.party.core.dao.write.copartner.BrokerageItemWriteDao;
import com.party.core.model.BaseModel;
import com.party.core.model.copartner.BrokerageItem;
import com.party.core.model.copartner.BrokerageListOutput;
import com.party.core.model.copartner.BrokerageStatusEnum;
import com.party.core.model.member.CopartnerAnalyzeQuery;
import com.party.core.service.copartner.IBrokerageItemService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: Administrator.
 * @Description: 佣金流水
 * @Date:Created in 2017/10/25 0025 上午 11:56.
 */
@Service
public class BrokerageItemService implements IBrokerageItemService {
    @Autowired
    private BrokerageItemReadDao brokerageItemReadDao;
    @Autowired
    private BrokerageItemWriteDao brokerageItemWriteDao;

    @Override
    public String insert(BrokerageItem t) {
        BaseModel.preInsert(t);
        boolean result = brokerageItemWriteDao.insert(t);
        if (result) {
            return t.getId();
        }
        return null;
    }

    @Override
    public boolean update(BrokerageItem t) {
        return brokerageItemWriteDao.update(t);
    }

    @Override
    public boolean deleteLogic(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return brokerageItemWriteDao.deleteLogic(id);
    }

    @Override
    public boolean delete(String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return brokerageItemWriteDao.delete(id);
    }

    @Override
    public boolean batchDeleteLogic(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return brokerageItemWriteDao.batchDeleteLogic(ids);
    }

    @Override
    public boolean batchDelete(Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return brokerageItemWriteDao.batchDelete(ids);
    }

    @Override
    public BrokerageItem get(String id) {
        return brokerageItemReadDao.get(id);
    }

    @Override
    public List<BrokerageItem> listPage(BrokerageItem t, Page page) {
        return brokerageItemReadDao.listPage(t, page);
    }

    @Override
    public List<BrokerageItem> list(BrokerageItem t) {
        return brokerageItemReadDao.listPage(t, null);
    }

    @Override
    public List<BrokerageItem> batchList(Set<String> ids, BrokerageItem t, Page page) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.EMPTY_LIST;
        }
        return brokerageItemReadDao.batchList(ids, new HashedMap(), page);
    }

    @Override
    public BrokerageItem getUnique(BrokerageItem brokerageItem) {
        return brokerageItemReadDao.getUnique(brokerageItem);
    }

    @Override
    public List<BrokerageListOutput> brokerageList(BrokerageItem brokerageItem, Page page) {
        return brokerageItemReadDao.brokerageList(brokerageItem, page);
    }


    @Override
    public List<BrokerageListOutput> lowerBrokerageDetailList(BrokerageItem brokerageItem, CopartnerAnalyzeQuery query,Page page) {
        query.transToDate(query);//先转换日期
        return brokerageItemReadDao.lowerBrokerageDetailList(brokerageItem, query, page);
    }

    @Override
    public List<BrokerageListOutput> newBrokerageList(BrokerageItem brokerageItem, Page page) {
        return brokerageItemReadDao.newBrokerageList(brokerageItem, page);
    }

    /**
     * 佣金流水金额
     *
     * @param partnerId 合伙人编号
     * @param onlyWith  1：可提现/已冻结 2：可提现 3：已冻结
     * @return
     */
    @Override
    public Double getSumTotal(String partnerId, Integer onlyWith) {
        Set<Integer> checkStatus = Sets.newHashSet();
        if (onlyWith == 1) {
            checkStatus.add(BrokerageStatusEnum.TWO_WITHDRAW.getCode()); // 可提现
            checkStatus.add(BrokerageStatusEnum.TWO_FREEZE.getCode()); // 已冻结
        } else if (onlyWith == 2) {
            checkStatus.add(BrokerageStatusEnum.TWO_WITHDRAW.getCode()); // 可提现
        } else if (onlyWith == 3) {
            checkStatus.add(BrokerageStatusEnum.TWO_FREEZE.getCode()); // 已冻结
        }

        Double totalBrokerage = brokerageItemReadDao.getSumTotal(partnerId, checkStatus); // 累计佣金
        return totalBrokerage == null ? 0d : totalBrokerage;
    }

    @Override
    public Double brokerageSum(String memberId, String endDate, String startDate, String activityId) {
        HashMap<String, Object> parameter = Maps.newHashMap();
        parameter.put("startDate", startDate);
        parameter.put("endDate", endDate);
        parameter.put("memberId", memberId);
        parameter.put("activityId", activityId);

        Set<Integer> checkStatus = Sets.newHashSet();
        checkStatus.add(BrokerageStatusEnum.TWO_FREEZE.getCode()); // 已冻结
        checkStatus.add(BrokerageStatusEnum.TWO_WITHDRAW.getCode()); // 可提现
        parameter.put("checkStatus", checkStatus);

        Double totalBrokerage = brokerageItemReadDao.brokerageSum(parameter);
        return totalBrokerage == null ? 0d : totalBrokerage;
//        if (totalBrokerage == null) {
//            return 0d;
//        } else {
//            checkStatus = Sets.newHashSet();
//            checkStatus.add(BrokerageStatusEnum.TWO_REJECT.getCode()); // 已拒绝
//            parameter.put("checkStatus", checkStatus);
//            Double errorBrokerage = brokerageItemReadDao.brokerageSum(parameter);
//            if (errorBrokerage != null) {
//                totalBrokerage = BigDecimalUtils.sub(totalBrokerage, errorBrokerage);
//                totalBrokerage = BigDecimalUtils.round(totalBrokerage, 2);
//            }
//            return totalBrokerage;
//        }
    }

    /**
     * 统计合伙人佣金
     *
     * @param memberId 用户编号
     * @param activityId 活动编号
     * @return  统计数
     */
    @Override
    public Double brokerageSum(String memberId, String activityId) {
        return this.brokerageSum(memberId, null, null, activityId);
    }

    @Override
    public Map<String, Object> getItemDetail(String itemId) {
        return brokerageItemReadDao.getItemDetail(itemId);
    }

    @Override
    public Double getMyBrokerage(String partnerId, Integer status, String startDate, String endDate, Boolean isContain) {
        Map<String,Object> params = Maps.newHashMap();
        params.put("partnerId", partnerId);
        params.put("status", status);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("isContain", isContain);
        return brokerageItemReadDao.getMyBrokerage(params);
    }

    @Override
    public Double getDisBrokerage(String partnerId, String activityId) {
        Map<String,Object> params = Maps.newHashMap();
        params.put("partnerId", partnerId);
        params.put("activityId", activityId);
        params.put("isContain", false);
        return brokerageItemReadDao.getMyBrokerage(params);
    }

    @Override
    public Double getMyBrokerageByTargetId(Map<String, Object> params) {
        return brokerageItemReadDao.getMyBrokerageByTargetId(params);
    }

    @Override
    public List<BrokerageListOutput> adminlowerBrokerageDetailList(BrokerageItem brokerageItem, CopartnerAnalyzeQuery query, Page page) {
        query.transToDate(query);//先转换日期
        return brokerageItemReadDao.adminlowerBrokerageDetailList(brokerageItem, query, page);
    }

    @Override
    public List<BrokerageItem> findByTargetId(String targetId) {
        BrokerageItem brokerageItem = new BrokerageItem();
        brokerageItem.setTargetId(targetId);
        return brokerageItemReadDao.listPage(brokerageItem, null);
    }

    @Override
    public List<BrokerageItem> getWithdrawDateNull(String activityId) {
        return brokerageItemReadDao.getWithdrawDateNull(activityId);
    }

    @Override
    public List<String> distinctActivityId() {
        return brokerageItemReadDao.distinctActivityId();
    }
}